﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMLGener.Interfaces;
using XMLGener.Data;

namespace XBasicModules
{
    /// <summary>
    /// 
    /// </summary>
    public class FloatRandomGenerator : ModuleBase, XMLGenerModuleInterface
    {
        private Random rnd = new Random();
        private decimal minNum = 0;
        private decimal maxNum = 0;
        private decimal value = 1000;
        private decimal actualNumber = 0;
        private decimal step = 1;
        private bool randomGenerating = false;
        #region XMLGenerModuleInterface Members

        private char[] trimNulls = { '0' };
        private char[] trimComma= { ',' };



        /// <summary>
        /// Return next random number;
        /// </summary>
        /// <returns></returns>

        public virtual string generate()
        {
            string result = null;
            if (randomGenerating)
            {
                if (rnd == null) return "";
                decimal dec = (Convert.ToDecimal(rnd.NextDouble()) * value + minNum);
                result = Convert.ToString(dec);
            }
            else
            {
                result = Convert.ToString(actualNumber);
                actualNumber += step;
                if (actualNumber > maxNum) actualNumber = minNum;

            }
            result = result.TrimEnd(trimNulls).TrimEnd(trimComma) ;
            return result;

        }

        /// <summary>
        /// Retrurn definitnion of this module
        /// </summary>
        /// <returns>xml data</returns>
        public virtual string getDefinition()
        {
            return
              "<function>" +
              "   <name>Float generator</name>" +
              "   <id>floatGenerator</id>" +
              "   <type>float</type>" +
              "   <attributes>" +
              "      <attribute>" +
              "         <name>Generating style</name>" +
              "         <id>order</id>" +
              "         <type>select</type>" +
              "         <items>" +
              "            <item name=\"Random numbers\" value=\"random\"/>" +
              "            <item name=\"Sequence (use step)\" value=\"sequence\"/>" +
              "         </items>" +
              "      </attribute>" +
              "      <attribute>" +
              "         <name>Minimum value</name>" +
              "         <id>minnum</id>" +
              "         <type>float</type>" +
              "      </attribute>" +
              "      <attribute>" +
              "         <name>Maximum value</name>" +
              "         <id>maxnum</id>" +
              "         <type>float</type>" +
              "      </attribute>" +
              "      <attribute>" +
              "         <name>Step</name>" +
              "         <id>step</id>" +
              "         <type>float</type>" +
              "      </attribute>" +
              "   </attributes>" +
              "</function>";
        }

        /// <summary>
        /// Set attributes for this module before generating.
        /// </summary>
        /// <param name="xmlParams"></param>
        /// <param name="parentDocument"></param>
        public virtual void setParameters(string xmlParams, XMLGenerDocument parentDocument)
        {
            if (!this.checkSettingsElement(xmlParams, parentDocument))
                return;
            this.minNum = Convert.ToDecimal(this.getParam("minnum"));
            maxNum = Convert.ToDecimal(this.getParam("maxnum"));
            if (minNum > maxNum) 
            {
                decimal tmp = minNum;
                minNum = maxNum;
                maxNum = tmp;
            }
            this.value = maxNum - minNum;
            string order = this.getParam("order");
            if (order == "random")
            {
                randomGenerating = true;
            }
            else
            {
                randomGenerating = false;
                this.actualNumber = minNum;
                step=Convert.ToDecimal(this.getParam("step"));
            }

        }
        #endregion
    }
}
